package cn.com.ccxe.web.form;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import cn.com.ccxe.core.entity.Keyword;

import com.ccxe.manage.service.CurrUserInfo;

public class FavoriteActionForm extends ActionForm {
	
	private int id;
	private int keywordId;
	private int websiteId;
	private int monitorTypeId;
	private String userId;
	private String method;
	private CurrUserInfo userInfo;
	private List<Keyword> keywords;
	
	public FavoriteActionForm(HttpServletRequest request) {
		super(request);
	}
	
	@Override
	protected void reset() {
		String str_id = getParemetes("id");
		String str_keywordId = getParemetes("keywordId");
		String str_websiteId = getParemetes("websiteId");
		String str_monitorTypeId = getParemetes("monitorTypeId");
		if (isEmpty(str_id)) {
			this.id = Integer.parseInt(str_id);
		}
		if (isEmpty(str_keywordId)) {
			this.keywordId = Integer.parseInt(str_keywordId);
		}
		if (isEmpty(str_websiteId)) {
			this.websiteId = Integer.parseInt(str_websiteId);
		}
		if (isEmpty(str_monitorTypeId)) {
			this.monitorTypeId = Integer.parseInt(str_monitorTypeId);
		}
		
		this.userId = getParemetes("userId");
		this.method = getParemetes("method");
		this.userInfo = (CurrUserInfo) getAttribute("currUser");
		this.keywords = setKeywords(getParameterValues("keywordIds"));
	}
	
	private boolean isEmpty(String str) {
		return !StringUtils.isEmpty(str);
	}

	public String getMethod() {
		return method;
	}
	public CurrUserInfo getUserInfo() {
		return userInfo;
	}
	public int getKeywordId() {
		return keywordId;
	}
	public int getWebsiteId() {
		return websiteId;
	}
	public String getUserId() {
		return userId;
	}
	public int getMonitorTypeId() {
		return monitorTypeId;
	}
	public int getId() {
		return id;
	}
	public List<Keyword> getKeywords() {
		return keywords;
	}
	
	private List<Keyword> setKeywords(String[] values) {
		if(values == null || values.length == 0) {
			return null;
		}
		List<Keyword> keywords = new ArrayList<Keyword>();
		for (String id : values) {
			int i = Integer.parseInt(id);
			if(i > 0) {
				keywords.add(new Keyword(i));
			}
		}
		if(keywords.size() > 0) {
			return keywords;
		}
		return null;
	}
	
}
